/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content) {
    uni.showToast({
        icon: 'none',
        title: content
    })
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
    return new Promise((resolve, reject) => {
        uni.showModal({
            title: '提示',
            content: content,
            cancelText: '取消',
            confirmText: '确定',
            success: function (res) {
                resolve(res)
            }
        })
    })
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
    let result = ''
    for (const propName of Object.keys(params)) {
        const value = params[propName]
        var part = encodeURIComponent(propName) + "="
        if (value !== null && value !== "" && typeof (value) !== "undefined") {
            if (typeof value === 'object') {
                for (const key of Object.keys(value)) {
                    if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
                        let params = propName + '[' + key + ']'
                        var subPart = encodeURIComponent(params) + "="
                        result += subPart + encodeURIComponent(value[key]) + "&"
                    }
                }
            } else {
                result += part + encodeURIComponent(value) + "&"
            }
        }
    }
    return result
}

/**判断数组为空*/
export function isEmptyArray(arr) {
    if (arr === null || arr === undefined || arr.length === 0) {
        return true
    }
    return false
}

//判断字符串为空
export function isEmptyString(str) {
    if (str === null || str === undefined || str === '') {
        return true
    }
    return false
}
//判断对象为空
export function isEmptyObject(obj) {
    if (obj === null || obj === undefined || obj === '') {
        return true
    }
    return false
}

//只取时间的年月日
export function getDay(date) {
    if (date === null || date === undefined || date === '') {
        return ''
    }
    return date.substring(0, 10)
}

//处理价格
export function formatPrice(price) {
    if (price === null || price === undefined || price === '') {
        return 0
    }
    if (typeof price === 'string') {
        price = parseFloat(price)
    }
    price = "￥" + price.toFixed(2)
    return price;
}

//数字超出9个用...代替
export function formatNumber(number) {
    if (number === null || number === undefined || number === '') {
        return 0
    }
    if (number.length > 9) {
        number = number.substring(0, 9) + "..."
    }
    return number
}

//将富文本中img中style设置为width:100%
export function formatRichText(html) {
    const content = html.replace(
        /<img/gi,
        '<img style="max-width:100%;height:auto;display:block;"'
    );
    return content;
}
//提取富文本中的图片地址
export function getImgUrl(richtext) {
    let imgList = [];
    richtext.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, (match, capture) => {
        imgList.push(capture);
    });
    return imgList;
}

//解析链接地址
export function getWxUrlParam(url) {
    let theRequest = {};
    if(url.indexOf("#") != -1){
        const str=url.split("#")[1];
        const strs=str.split("&");
        for (let i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
        }
    }else if(url.indexOf("?") != -1){
        const str=url.split("?")[1];
        const strs=str.split("&");
        for (let i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}